package com.huadao.pda.main;

import android.device.DeviceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.huadao.pda.R;
import com.huadao.pda.base.BaseScanActivity;
import com.huadao.pda.base.OnScanResult;
import com.huadao.pda.me.MineFrag;
import com.huadao.pda.project.projectList.ProjectListDF;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseScanActivity {

    @BindView(R.id.iv_home)
    ImageView ivHome;
    @BindView(R.id.tv_home)
    TextView tvHome;
    @BindView(R.id.iv_mine)
    ImageView ivMine;
    @BindView(R.id.tv_mine)
    TextView tvMine;

    private FragmentManager fragmentManager;
    private Fragment currentFrag;
    private ProjectListDF projectListDF;
    private MineFrag mineFrag;
    private DeviceManager deviceManager;

    @Override
    protected void init() {
        super.init();
        fragmentManager = getSupportFragmentManager();
        if (projectListDF == null) {
            projectListDF = new ProjectListDF();
        }
        showFragment(projectListDF);
        title.setText(getString(R.string.home));
        deviceManager=new DeviceManager();
        deviceManager.enableHomeKey(false);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_main;
    }

    @OnClick({R.id.ll_home, R.id.ll_mine})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_home:
                ivHome.setImageResource(R.drawable.home_selected);
                tvHome.setTextColor(getResources().getColor(R.color.purple));
                ivMine.setImageResource(R.drawable.mine_unselected);
                tvMine.setTextColor(getResources().getColor(R.color.text_tab_unselected));
                if (projectListDF == null) {
                    projectListDF = new ProjectListDF();
                }
                showFragment(projectListDF);
                title.setText(R.string.home);

                break;
            case R.id.ll_mine:
                ivHome.setImageResource(R.drawable.home_unselected);
                tvHome.setTextColor(getResources().getColor(R.color.text_tab_unselected));
                ivMine.setImageResource(R.drawable.mine_selected);
                tvMine.setTextColor(getResources().getColor(R.color.purple));
                if (mineFrag == null) {
                    mineFrag = new MineFrag();
                }
                showFragment(mineFrag);
                title.setText(getString(R.string.mine));

                break;
        }
    }

    private void showFragment(Fragment fragment) {
        if (fragment == null) {
            return;
        }
        if (currentFrag == null || currentFrag != fragment) {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            if(currentFrag!=null) {
                transaction.hide(currentFrag);
            }
            if (!fragment.isAdded()) {
                transaction.add(R.id.fl_container, fragment).show(fragment).commit();
            } else {
                transaction.show(fragment).commit();
            }
            currentFrag = fragment;
        }
    }

    @Override
    public void onScanResult(String barcode) {
        stopScan();
        Log.i(TAG,barcode);
        List<Fragment> fragmentList=fragmentManager.getFragments();
        for(Fragment fragment:fragmentList){
            if(fragment instanceof OnScanResult){
                ((OnScanResult) fragment).onScanResult(barcode);
            }
        }
    }

    //退出时的时间
    private long mExitTime;
    //对返回键进行监听
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            showToast("再按一次退出");
            mExitTime = System.currentTimeMillis();
        } else {
            finish();
//            System.exit(0);
        }
    }

    @Override
    protected void onDestroy() {
        deviceManager.enableHomeKey(true);
        super.onDestroy();
    }
}
